package cc.lucien.controller;

import cc.lucien.entity.GoodType;
import cc.lucien.entity.GoodTypeVO;
import cc.lucien.service.GoodTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/goodType")
public class GoodTypeController {

    @Autowired
    private GoodTypeService goodTypeService;

    @GetMapping("/show")
    @ResponseBody
    public GoodTypeVO show() {
        GoodTypeVO goodTypeVO = new GoodTypeVO();
        goodTypeVO.setCode(0);
        goodTypeVO.setMsg("");
        goodTypeVO.setCount(1L);
        goodTypeVO.setData(goodTypeService.findAll());
        return goodTypeVO;
    }

    @PostMapping("/save")
    public String save(GoodType goodType) {
        goodTypeService.save(goodType);
        return "goodTypeTable";
    }

    @GetMapping("/toUpdate/{id}")
    public ModelAndView toUpdate(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("goodTypeUpdate");
        modelAndView.addObject("goodType", goodTypeService.findById(id));
        return modelAndView;
    }

    @PostMapping("/update")
    public String update(GoodType goodType) {
        goodTypeService.update(goodType);
        return "goodTypeTable";
    }

    @GetMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") Integer id) {
        goodTypeService.deleteById(id);
        return "goodTypeTable";
    }

}
